package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


// 菜品管理
@RestController
public class DishController {

    @Autowired
    private DishService dishService;


    //分页条件查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            String name){ //接收参数
        //调用service查询
        Page<Dish> page = dishService.findByPage(pageNum,pageSize,name);
        //返回结果
        return ResultInfo.success(page);
    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        //调用service
        dishService.save(dish);
        //返货结果
        return ResultInfo.success();
    }

    //菜品回显
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        //service 根据菜品id查询
        Dish dish = dishService.findById(id);
        //返回成功消息
        return  ResultInfo.success(dish);
    }

    //菜品修改
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        //调用service保存
        dishService.update(dish);
        //返回结果
        return ResultInfo.success();
    }

    //启停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status")Integer status,
                                   @RequestParam List<Long> ids){
        //调用service
        dishService.updateStatus(status,ids);
        //返回结果
        return ResultInfo.success();
    }

    //删除菜品
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids){
        //调用service
        dishService.deleteBatchIds(ids);
        //返回
        return ResultInfo.success();
    }
}
